#include <stdlib.h>
#include "mls.h"

/* do sime interesting stuff with arrays */
void run_tests(int argc, char **argv);

/* helpers */
void fill_float_array(int h);
void dump_float_array(int h);


int main(int argc, char **argv)
{
  m_init();                     /* initialize safe-array handling */

  run_tests(argc,argv);

  m_destruct();                 /* free memory allocated by safe-array handling lib "mls" */
  return 0;
}






void run_tests(int argc, char **argv)
{
                                /* allocate, put data, use data, free */
  int m;                        /* handle for my array */

  int s;                        /* array of some float numbers */
  
  int u,i,h;                    /* this is "i" and "u" and "h" */

  float *ptr_to_float;          /* this points to ... */
                                /* you dont expect me to explain this, do you? */ 

  int *ptr_to_handle;

  /* some numbers, integer type */
  m = m_create( 5, sizeof(int) );

  /* create an array of float's with different length */ 
  for(i=0;i<10;i++) {
   h = m_create( 1, sizeof(float) );
   fill_float_array(h);
   m_put(m,&h);
  }

  printf( "dump some arrays \n");
  m_foreach( m,i,ptr_to_handle) {
    h = *ptr_to_handle;         /* shortcut */
    printf("dumping entry=%d, len=%d\n", i, m_len(h));
    dump_float_array(h);
    printf("\n\n");
  }

  /* if you want you can free the allocated memory now */
  m_foreach( m,i,ptr_to_handle) m_free( *ptr_to_handle);
  m_free(m);
}


/** h : safe-array handle for array[0..num-1] of float */ 
void fill_float_array(int h)
{
  float d;
  int num = rand() % 10;
  m_clear(h);
  while(num--) { d=rand() *1.0 / RAND_MAX; m_put(h,&d); }
}


void dump_float_array(int h)
{
  int i;
  float *ptr_to_float;
  m_foreach( h, i, ptr_to_float )
    printf("%f ", *ptr_to_float);
}
